/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of libgp2x.

    libgp2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libgp2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libgp2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <stdio.h>

#include "gp2x_internal.h"

#define INTERNAL_JOYSTICK_UP        (1<<0)
#define INTERNAL_JOYSTICK_UPLEFT    (1<<1)
#define INTERNAL_JOYSTICK_LEFT      (1<<2)
#define INTERNAL_JOYSTICK_DOWNLEFT  (1<<3)
#define INTERNAL_JOYSTICK_DOWN      (1<<4)
#define INTERNAL_JOYSTICK_DOWNRIGHT (1<<5)
#define INTERNAL_JOYSTICK_RIGHT     (1<<6)
#define INTERNAL_JOYSTICK_UPRIGHT   (1<<7)
#define INTERNAL_JOYSTICK_PUSH      (1<<11)
#define INTERNAL_START              (1<< 8)
#define INTERNAL_SELECT             (1<< 9)
#define INTERNAL_TRIGGER_LEFT       (1<<10)
#define INTERNAL_TRIGGER_RIGHT      (1<<11)
#define INTERNAL_A                  (1<<12)
#define INTERNAL_B                  (1<<13)
#define INTERNAL_X                  (1<<14)
#define INTERNAL_Y                  (1<<15)
#define INTERNAL_VOL_DOWN           (1<< 7)
#define INTERNAL_VOL_UP             (1<< 6)

void GP2x_input_init() {
}

unsigned int GP2x_input_poll() {
    unsigned short r1 = ~(GP2x_register(GP2x_REG_BUTTONS_1));
    unsigned short r2 = ~(GP2x_register(GP2x_REG_BUTTONS_2));
    unsigned short r3 = ~(GP2x_register(GP2x_REG_JOYSTICK));

    unsigned int value = 0x0000;

    if(r1 & INTERNAL_START) { value = value | START; }
    if(r1 & INTERNAL_SELECT) { value = value | SELECT; }
    if(r1 & INTERNAL_A) { value = value | BUTTON_A; }
    if(r1 & INTERNAL_B) { value = value | BUTTON_B; }
    if(r1 & INTERNAL_X) { value = value | BUTTON_X; }
    if(r1 & INTERNAL_Y) { value = value | BUTTON_Y; }
    if(r1 & INTERNAL_TRIGGER_LEFT) { value = value | TRIGGER_LEFT; }
    if(r1 & INTERNAL_TRIGGER_RIGHT) { value = value | TRIGGER_RIGHT; }

    if(r2 & INTERNAL_VOL_DOWN) { value = value | VOLUME_DOWN; }
    if(r2 & INTERNAL_VOL_UP) { value = value | VOLUME_UP; }
    if(r2 & INTERNAL_JOYSTICK_PUSH) { value = value | JOYSTICK_PUSH; }

    if(r3 & INTERNAL_JOYSTICK_UP) { value = value | JOYSTICK_UP; }
    else if(r3 & INTERNAL_JOYSTICK_LEFT) { value = value | JOYSTICK_LEFT; }
    else if(r3 & INTERNAL_JOYSTICK_DOWN) { value = value | JOYSTICK_DOWN; }
    else if(r3 & INTERNAL_JOYSTICK_RIGHT) { value = value | JOYSTICK_RIGHT; }
    else {
        if(r3 & INTERNAL_JOYSTICK_UPLEFT) { value = value | JOYSTICK_UPLEFT; }
        else if(r3 & INTERNAL_JOYSTICK_DOWNLEFT) { value = value | JOYSTICK_DOWNLEFT; }
        else if(r3 & INTERNAL_JOYSTICK_DOWNRIGHT) { value = value | JOYSTICK_DOWNRIGHT; }
        else if(r3 & INTERNAL_JOYSTICK_UPRIGHT) { value = value | JOYSTICK_UPRIGHT; }
    }

    return value;
}
